<?php

namespace App\Http\Controllers;
use App\DegModel;
use App\TeacherModel;
use App\SubModel;
use Illuminate\Http\Request;

class TeacherController extends Controller{
    public function teacher(Request$request){
        $deg = DegModel::get();
        $sub = SubModel::get();


        $id=$request->input(['id']);
        $xx=TeacherModel::where('id',$id)->first();
        return view('teacher',compact('xx',"deg","sub"));
    }

    public function teacher_add(Request$request){
        $input=$request->input();
        $id = $input['id'];
        unset($input['_token']);
        $img=$request->file('pic');
        $img_name=$img->getClientOriginalName();
        $img->move(public_path('uploads/'),$img_name);
        $input['pic']='uploads/'.$img_name;
        if ($id==0){
            $ok=TeacherModel::create($input);
            return $ok?'قوشۇش مۇۋاپىقيەتلىك بولدى!':'قوشۇش مەغلۇپ بولدى!';
        }else{
            $maz = TeacherModel::where('id',$id)->first();
            $ok=$maz->update($input);
            return $ok?'ئۆزگەرتىش مۇۋاپىقيەتلىك بولدى!':'ئۆزگەرتىش مەغلۇپ بولدى!';
        }
    }

    public function teacher_table(){
        $cols = ['teacher.name','teacher.age','teacher.pic', 'teacher.created_at','teacher.updated_at',
            'unwan.unwan as teacher_unwan','pan_tur.pan as teacher_subject'];
        $uqur=TeacherModel::leftjoin("unwan","teacher.unwan","unwan.id")
            ->leftjoin("pan_tur","teacher.pan","pan_tur.id")->get($cols);
        return view('teacher_table',compact("uqur"));

    }

    public function teacher_delete(Request$request){
        $id = $request->input("id");
        $ok=TeacherModel::where('id',$id)->delete();
        return$ok?'删除成功！':'删除失败！';

    }



}
